package com.lenovo.smarttraffic.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.JsonRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.lenovo.smarttraffic.R;
import com.lenovo.smarttraffic.bean.GongJiaoBean_7;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class Activity_4 extends AppCompatActivity {
    private ImageView icon;
    private TextView title;
    private TextView dingdan;
    private ListView a_4_exlv1;
    RequestQueue queue;
    MyAdapter adapter;
    List<GongJiaoBean_7.ROWSDETAILBean> beans = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.a_4);
        initView();

        getdata();

    }

    private void getdata() {

        JSONObject object = new JSONObject();
        try {
            object.put("UserName", "user1");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        JsonObjectRequest request = new JsonObjectRequest(JsonRequest.Method.POST, "http://192.168.1.107:8088/transportservice/action/GetBusInfo.do", object, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                beans = new Gson().fromJson(jsonObject.toString(), GongJiaoBean_7.class).getROWS_DETAIL();
                adapter.notifyDataSetChanged();

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        });
        queue.add(request);

    }

    private void initView() {
        icon = (ImageView) findViewById(R.id.icon);
        title = (TextView) findViewById(R.id.title);
        dingdan = (TextView) findViewById(R.id.dingdan);
        a_4_exlv1 = (ListView) findViewById(R.id.a_4_exlv1);

        icon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        title.setText("定制班车");
        queue = Volley.newRequestQueue(Activity_4.this);
        adapter = new MyAdapter();
        a_4_exlv1.setAdapter(adapter);

        a_4_exlv1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                ArrayList arrayList = new ArrayList();
                arrayList.add(i);
                arrayList.add(beans.get(i).getTime().get(0).getSite());
                arrayList.add(beans.get(i).getTime().get(1).getSite());
                arrayList.add(beans.get(i).getTicket());
                arrayList.add(beans.get(i).getMileage());
                startActivity(new Intent(Activity_4.this,Activity_4_2.class).putStringArrayListExtra("data",arrayList));
            }
        });

    }

    class MyAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return beans.size();
        }

        @Override
        public Object getItem(int i) {
            return beans.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {

            view = getLayoutInflater().inflate(R.layout.a_4_fex, null);

            ViewHolder holder = new ViewHolder(view);

            int a=i+1;
            holder.a_4_fex_tv1.setText(a+"路");
            holder.a_4_fex_tv2.setText(beans.get(i).getTime().get(0).getSite()+"-"+beans.get(i).getTime().get(1).getSite());
            holder.a_4_fex_tv3.setText("票价：￥"+beans.get(i).getTicket()+".0  里程："+beans.get(i).getMileage()+"Km");
            holder.a_4_fex_tv4.setText(beans.get(i).getTime().get(0).getStarttime().substring(0,5));
            holder.a_4_fex_tv5.setText(beans.get(i).getTime().get(1).getEndtime().substring(0,5));

            return view;
        }

        class ViewHolder {
            public View rootView;
            public TextView a_4_fex_tv1;
            public TextView a_4_fex_tv2;
            public TextView a_4_fex_tv3;
            public TextView a_4_fex_tv4;
            public TextView a_4_fex_tv5;

            public ViewHolder(View rootView) {
                this.rootView = rootView;
                this.a_4_fex_tv1 = (TextView) rootView.findViewById(R.id.a_4_fex_tv1);
                this.a_4_fex_tv2 = (TextView) rootView.findViewById(R.id.a_4_fex_tv2);
                this.a_4_fex_tv3 = (TextView) rootView.findViewById(R.id.a_4_fex_tv3);
                this.a_4_fex_tv4 = (TextView) rootView.findViewById(R.id.a_4_fex_tv4);
                this.a_4_fex_tv5 = (TextView) rootView.findViewById(R.id.a_4_fex_tv5);
            }

        }
    }

}
